package com.g2.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.g2.admin.model.po.AuthRolePo;
import com.g2.admin.model.po.UserAccountPo;
import com.g2.admin.model.qo.AccountQo;
import com.g2.admin.model.qo.RoleQo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.g2.admin.model.po.AuthRolePo;
import com.g2.admin.dao.mapper.IAuthRoleMapper;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AuthRoleService {
    @Autowired
    private IAuthRoleMapper authroleMapper;

    public AuthRolePo getAuthrole(long id) {
        return authroleMapper.selectById(id);
    }

    public void delAuthrole(long id) {
        authroleMapper.deleteById(id);
    }

    public void addAuthrole(AuthRolePo authrole) {
        authroleMapper.insert(authrole);
    }

    public void updateAuthrole(AuthRolePo authrole) {
        authroleMapper.updateById(authrole);
    }

    /**
     * 获取所有角色列表，不分页
     */
    public List<AuthRolePo> getAuthRoles(RoleQo roleQo) {
        return authroleMapper.selectList(getQw(roleQo));
    }

    /**
     * 获取所有角色列表，并分页
     */
    public IPage<AuthRolePo> getAuthRoleAndPage(RoleQo roleQo) {
        return authroleMapper.selectPage(new Page<>(roleQo.getPageNo(), roleQo.getSize()), getQw(roleQo));
    }

    private LambdaQueryWrapper<AuthRolePo> getQw(RoleQo roleQo) {
        return new QueryWrapper<AuthRolePo>().lambda()
                .eq(StringUtils.isNotEmpty(roleQo.getSystemCode()), AuthRolePo::getSystemCode, roleQo.getSystemCode())
                .eq(StringUtils.isNotEmpty(roleQo.getCode()), AuthRolePo::getCode, roleQo.getCode())
                .like(StringUtils.isNotEmpty(roleQo.getName()), AuthRolePo::getName, roleQo.getName())
                .eq(null != roleQo.getOrgId(), AuthRolePo::getOrgId, roleQo.getOrgId())
                .eq(null != roleQo.getStatus(), AuthRolePo::getStatus, roleQo.getStatus())
                ;
    }
}
